package com.tdam2011.grupo05.database.manager;

import java.util.LinkedList;
import java.util.List;

import com.tdam2011.grupo05.database.dao.AccountDao;
import com.tdam2011.grupo05.database.dao.ContactWebDao;
import com.tdam2011.grupo05.database.dao.LogDao;
import com.tdam2011.grupo05.database.dao.MessageWebDao;
import com.tdam2011.grupo05.database.dao.ThreadWebDao;
import com.tdam2011.grupo05.entities.account.Account;
import com.tdam2011.grupo05.entities.contact.ContactWeb;
import com.tdam2011.grupo05.entities.logs.LogImp;
import com.tdam2011.grupo05.entities.message.MessageWeb;
import com.tdam2011.grupo05.entities.thread.ThreadWeb;

import android.content.Context;
import android.database.Cursor;

public final class DataManager implements Manager {

	private Context mContext;

	private final MessageWebDao messageWebDao;
	private final ThreadWebDao threadWebDao;
	private final ContactWebDao contactWebDao;
	private final AccountDao accountDao;
	private final LogDao logDao;

	private static DataManager sInstance = null;

	private DataManager(Context context) {
		mContext = context;
		messageWebDao = new MessageWebDao(mContext);
		threadWebDao = new ThreadWebDao(mContext);
		contactWebDao = new ContactWebDao(mContext);
		accountDao = new AccountDao(mContext);
		logDao = new LogDao(mContext);
	}

	public static DataManager getInstance(Context context) {
		if (sInstance == null) {
			sInstance = new DataManager(context);
		}
		return sInstance;
	}

	@Override
	public void deleteMessage(MessageWeb m) {
		messageWebDao.delete(m);
	}

	@Override
	public List<MessageWeb> getAllMessages() {
		return messageWebDao.getAll();
	}

	@Override
	public MessageWeb getMessage(long messageId) {
		return messageWebDao.get(messageId);
	}

	@Override
	public long saveMessage(MessageWeb m) {
		return messageWebDao.save(m);
	}

	@Override
	public void updateMessage(MessageWeb m) {
		messageWebDao.update(m);
	}

	@Override
	public Cursor getMessageCursor() {
		return messageWebDao.getCursor(null, null, null);
	}

	@Override
	public MessageWeb getMessageFromCursor(Cursor c) {
		return messageWebDao.buildMessageFromCursor(c);
	}

	@Override
	public LinkedList<MessageWeb> getUnsendedMessage() {
		return messageWebDao.getUnsendedMessages();
	}

	@Override
	public void deleteThread(ThreadWeb t) {
		threadWebDao.delete(t);
	}

	@Override
	public List<ThreadWeb> getAllThread() {
		return threadWebDao.getAll();
	}

	@Override
	public ThreadWeb getThread(long threadId) {
		return threadWebDao.get(threadId);
	}

	@Override
	public ThreadWeb getThreadByContact(long idContact) {
		return threadWebDao.getByContact(idContact);
	}

	@Override
	public ThreadWeb getThreadByContactName(String name) {
		return threadWebDao.getThreadWebByName(name);
	}

	@Override
	public ThreadWeb getThreadByCursor(Cursor c) {
		return threadWebDao.buildThreadWeb(c);
	}

	@Override
	public Cursor getThreadCursor() {
		return threadWebDao.getCursor(null, null, null);
	}

	@Override
	public long saveThread(ThreadWeb t) {
		return threadWebDao.save(t);
	}
	
	@Override
	public void updateThread(ThreadWeb t) {
		threadWebDao.update(t);
	}

	@Override
	public void deleteContactWeb(ContactWeb c) {
		contactWebDao.delete(c);
	}

	@Override
	public List<ContactWeb> getAllContact() {
		return contactWebDao.getAll();
	}

	@Override
	public ContactWeb getContactWeb(long contactId) {
		return contactWebDao.get(contactId);
	}

	@Override
	public Cursor getContactWebCursor() {
		return contactWebDao.getCursor(null, null, null);
	}

	@Override
	public ContactWeb getContactWebByName(String contactName) {
		return contactWebDao.getContactWebByName(contactName);
	}

	@Override
	public long saveContactWeb(ContactWeb c) {
		return contactWebDao.save(c);
	}

	@Override
	public void updateContactWeb(ContactWeb c) {
		contactWebDao.update(c);
	}

	@Override
	public void deleteAccount(Account a) {
		accountDao.delete(a);
	}

	@Override
	public Account getAccount(long idAccount) {
		return accountDao.get(idAccount);
	}

	@Override
	public Account get(String user, String pass) {
		return accountDao.get(user, pass);
	}

	@Override
	public Account get(String user) {
		return accountDao.get(user);
	}

	@Override
	public Cursor getAccountCursor(String selection, String[] selectionArgs,
			String sortOrder) {
		return accountDao.getCursor(selection, selectionArgs, sortOrder);
	}

	@Override
	public List<Account> getAllAccount() {
		return accountDao.getAll();
	}

	@Override
	public long saveAccount(Account a) {
		return accountDao.save(a);
	}

	@Override
	public void updateAccount(Account a) {
		accountDao.update(a);

	}

	@Override
	public void deleteLog(LogImp l) {
		logDao.delete(l);

	}

	@Override
	public List<LogImp> getAllLog() {
		return logDao.getAll();
	}

	@Override
	public LogImp getLog(long idLog) {
		return logDao.get(idLog);
	}

	@Override
	public Cursor getLogCursor(String selection, String[] selectionArgs,
			String sortOrder) {
		return logDao.getCursor(selection, selectionArgs, sortOrder);
	}

	@Override
	public long saveLog(LogImp l) {
		return logDao.save(l);
	}

	@Override
	public void updateLog(LogImp l) {
		logDao.update(l);
	}

}
